package com.longcai.controller.nearbyshop;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.longcai.advice.RollBackExecption;
import com.longcai.bean.ApiResult;
import com.longcai.constants.BaseConstants;
import com.longcai.entity.nearby.ClyjNearbyShopsGoods;
import com.longcai.entity.nearby.vo.ClyjNearbyShopGoodsDeleteVo;
import com.longcai.entity.nearby.vo.ClyjNearbyShopsGoodsQueryVo;
import com.longcai.entity.nearby.vo.ClyjNearbyGoodsTypeDeleteVo;
import com.longcai.entity.nearby.vo.ClyjNearbyGoodsTypeVo;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.nearbyshop.ClyjNearbyShopsGoodsService;

import io.swagger.annotations.ApiOperation;
/****
 * 商品管理后台
 * 外送模块
 * @author kh
 *
 */
@RestController
@RequestMapping("/clyjNearbyShopsGoods")
public class ClyjNearbyShopsGoodsController {


    @Autowired
    private ClyjNearbyShopsGoodsService clyjNearbyShopsGoodsService;

    /****
     * 查询商品列表
     */
    
    @ApiOperation(value = "商品列表")
    @RequestMapping("/queryShopsGoodsList")
	@ResponseBody
    public PageTableResponse queryShopsGoodsList(@RequestBody ClyjNearbyShopsGoodsQueryVo clyjNearbyShopsGoodsQueryVo, JSONObject param) {
        int  start =param.getInteger("start");
        int  length=param.getInteger("length");
    	return clyjNearbyShopsGoodsService.queryShopsGoodsList(clyjNearbyShopsGoodsQueryVo, start, length);

    }
    
    /****
	 * 查询 外送商品列表
	 * 选项卡    的数量
	 * @return
	 * 
	 */
    @ApiOperation(value = "商品列表---选项卡 ")
    @RequestMapping("/queryShopsGoodsMode")
	@ResponseBody
    public ApiResult<?> queryShopsGoodsMode() {
    	 Map<String ,Integer> r	= clyjNearbyShopsGoodsService.queryShopsGoodsMode();
    	 return  ApiResult.success(r);
    }
    
    /****
	   * 修改外送商品
	   * @param ClyjNearbyShopsGoods
	   * @return
	 * @throws Exception 
	 */
	  @RequestMapping("/updateNearbyShopsGoods")
	  @ResponseBody
	  public ApiResult<?> updateNearbyShopsGoods(@RequestBody ClyjNearbyShopsGoods clyjNearbyShopsGoods,MultipartFile[] goodsPicture) throws Exception{
		 ApiResult<?> r=  clyjNearbyShopsGoodsService.updateNearbyShopsGoods(clyjNearbyShopsGoods,goodsPicture);
		  return r;
	  }
	  /****
	   * 保存外送商品
	   * @param ClyjNearbyShopsGoods
	   * @return
	 * @throws Exception 
	 */
	  @RequestMapping("/saveNearbyShopsGoods")
	  @ResponseBody
	  public ApiResult<?> saveNearbyShopsGoods(@RequestBody ClyjNearbyShopsGoods clyjNearbyShopsGoods ,MultipartFile[] goodsPicture) throws Exception{
		 ApiResult<?> r=  clyjNearbyShopsGoodsService.saveNearbyShopsGoods(clyjNearbyShopsGoods,goodsPicture);
		  return r;
	  }
    /****
     * 外送商品列表
     * 下架单个
     * @param list
     * @return
     * @throws RollBackExecption 
     */
    @RequestMapping("/pullOffShelves")
    @ResponseBody
    public ApiResult<?> pullOffShelves(@RequestBody  String goodsId) throws RollBackExecption{
    	ApiResult<?> r=	clyjNearbyShopsGoodsService.pullOffShelves(goodsId);
    	return ApiResult.success();
    }
    /****
     * 外送商品列表
     * 删除单个
     * @param list
     * @return
     * @throws RollBackExecption 
     */
    @RequestMapping("/deleteNearbyShopsGoods")
    @ResponseBody
    public ApiResult<?>deleteNearbyShopsGoods(@RequestBody JSONObject param) throws RollBackExecption{
    	ApiResult<?> r=	clyjNearbyShopsGoodsService.deleteNearbyShopsGoods(param.getString("goodsId"));
    	return ApiResult.success();
    }
    
    /****
     * 外送商品列表
     * 批量下架
     * @param list
     * @return
     * @throws RollBackExecption 
     */
    @RequestMapping("/batchPullOffShelves")
    public ApiResult<?> batchPullOffShelves(@RequestBody ClyjNearbyShopGoodsDeleteVo  vo) throws RollBackExecption{
    	List<Map<String, String>> goodsIdList=vo.getGoodsIdList();
    	ApiResult<?> r=	clyjNearbyShopsGoodsService.batchPullOffShelves(goodsIdList);
    	if(!r.getCode().equals(BaseConstants.SUCCESS)){
    		return r;
    				
    	}
    	return ApiResult.success();
    }
    /****
     * 外送商品列表
     * 批量删除
     * @param list
     * @return
     * @throws RollBackExecption 
     */
    @RequestMapping("/batchDeleteNearbyShopsGoods")
    @ResponseBody
    public ApiResult<?> batchDeleteNearbyShopsGoods(@RequestBody  ClyjNearbyShopGoodsDeleteVo  vo) throws RollBackExecption{
    	List<Map<String, String>> goodsIdList=vo.getGoodsIdList();
    	ApiResult<?> r=	clyjNearbyShopsGoodsService.batchDeleteNearbyShopsGoods(goodsIdList);
    	if(!r.getCode().equals(BaseConstants.SUCCESS)){
    		return r;
    	}
    	return ApiResult.success();
    }
    /****
     * 外送商品列表
     * 查询商品类型
     * @param list
     * @return
     */
    @RequestMapping("/queryGoodsType")
    @ResponseBody
    public PageTableResponse queryGoodsType(@RequestBody JSONObject param){
	    int  start =param.getInteger("start");
        int  length=param.getInteger("length");
        PageTableResponse page=	clyjNearbyShopsGoodsService.queryGoodsType(start,length);
    	return page;
    }
    
    /****
     * 外送商品列表
     * 修改商品类型
     * @param list
     * @return
     */
    @RequestMapping("/updateGoodsType")
    @ResponseBody
    public ApiResult<?>  updateGoodsType(@RequestBody  ClyjNearbyGoodsTypeVo clyjNearbyGoodsTypeVo){
    	ApiResult<?>  r=	clyjNearbyShopsGoodsService.updateGoodsType(clyjNearbyGoodsTypeVo);
    	if(!r.getCode().equals(BaseConstants.SUCCESS)){
    		return r;
    	}
    	return ApiResult.success();
    }
    
    /****
     * 外送商品列表
     * 修改商品类型状态
     * @param list
     * @return
     */
    @RequestMapping("/updateGoodsTypeState")
    @ResponseBody
    public ApiResult<?>  updateGoodsTypeState(@RequestBody JSONObject param){
    	String state=param.getString("showState");
    	String id=param.getString("id");
    	ApiResult<?>  r=	clyjNearbyShopsGoodsService.updateGoodsTypeState(state,id);
    	if(!r.getCode().equals(BaseConstants.SUCCESS)){
    		return r;
    	}
    	return ApiResult.success();
    }
    /****
     * 外送商品列表
     * 添加商品类型
     * @param list
     * @return
     */
    @RequestMapping("/addGoodsType")
    @ResponseBody
    public ApiResult<?>  addGoodsType(@RequestBody  ClyjNearbyGoodsTypeVo clyjNearbyGoodsTypeVo){
    	ApiResult<?>  r=	clyjNearbyShopsGoodsService.addGoodsType(clyjNearbyGoodsTypeVo);
    	if(!r.getCode().equals(BaseConstants.SUCCESS)){
    		return r;
    	}
    	return ApiResult.success();
    }
    /****
     * 外送商品列表
     * 删除商品类型
     * @param list
     * @return
     */
    @RequestMapping("/deleteGoodsType")
    @ResponseBody
    public ApiResult<?>  deleteGoodsType(@RequestBody JSONObject param){
    	String goodsId=(String) param.get("id");
    	ApiResult<?>  r=	clyjNearbyShopsGoodsService.deleteGoodsType(goodsId);
    	if(!r.getCode().equals(BaseConstants.SUCCESS)){
    		return r;
    	}
    	return ApiResult.success();
    }
    
    /****
     * 外送商品列表
     * 批量删除商品类型
     * @param list
     * @return
     * @throws RollBackExecption 
     */
    @RequestMapping("/batchDeleteGoodsType")
    @ResponseBody
    public ApiResult<?>  batchDeleteGoodsType(@RequestBody ClyjNearbyGoodsTypeDeleteVo clyjNearbyGoodsTypeDeleteVo) throws RollBackExecption{
    	List<Map<String,String>> typeIdList=clyjNearbyGoodsTypeDeleteVo.getTypeIdList();
    	ApiResult<?> r=	clyjNearbyShopsGoodsService.batchDeleteGoodsType(typeIdList);
    	if(!r.getCode().equals(BaseConstants.SUCCESS)){
    		return r;
    	}
    	return ApiResult.success();
    }
    /****
	   * 单个上架
	   * @param 
	   * @return
	 * @throws Exception 
	 */
	  @RequestMapping("/putOnsale")
	  @ResponseBody
	  public ApiResult<?> putOnsale(@RequestBody JSONObject param ) throws Exception{
		  String goodsId=param.getString("goodsId");
		  String type=param.getString("type");
		  ApiResult<?> r=  clyjNearbyShopsGoodsService.putOnsale(goodsId,type);
		  return r;
	  }
}

